// 语言文本对象
const languageData = {
    "zh-tw": {
      text_1: "岩盤浴太空艙",
      text_3: "後艙加熱器",
      text_6: "前艙加熱器",
      text_9: "岩盤加熱器",
      text_12: "語言",
      text_17: "UV殺菌燈",
      text_18: "負離子產生器",
      text_19: "溫度單位",
      text_31: "岩盤定時加熱",
      text_22: "前壓力感測器",
      text_25: "後壓力感測器",
      text_28: "有效電能",
      paragraph_1:"溫度設定",
      paragraph_2:"開始時間",
      paragraph_3:"結束時間",
    },
    "zh-cn": {
      text_1: "岩盘浴太空舱",
      text_3: "后舱加热器",
      text_6: "前舱加热器",
      text_9: "岩盘加热器",
      text_12: "语言",
      text_17: "UV杀菌灯",
      text_18: "负离子产生器",
      text_19: "温度单位",
      text_31: "岩盘定时加热",
      text_22: "前压力电阻值",
      text_25: "后压力传感器",
      text_28: "有效电能",
      paragraph_1:"温度设定",
      paragraph_2:"开始时间",
      paragraph_3:"结束时间",
    },
    
    "en-us": {
      text_1: "Stone SPA",
      text_3: "Rear",
      text_6: "Front",
      text_9: "Bed",
      text_12: "Language",
      text_17: "UV lamp",
      text_18: "Negative ion generator",
      text_19: "Temperature unit",
      text_31: "Bed set time",
      text_22: "Front pressure",
      text_25: "Rear pressure",
      text_28: "ePower",
      paragraph_1:"Temp",
      paragraph_2:"Start",
      paragraph_3:"End",
    },
    "ja-jp": {
      text_1: "岩盤浴",
      text_3: "後部",
      text_6: "前部",
      text_9: "ベッド",
      text_12: "言語",
      text_17: "UVランプ",
      text_18: "マイナスイオン発生器",
      text_19: "温度単位",
      text_31: "ベッド時刻設定",
      text_22: "フロント圧力センサー",
      text_25: "後部圧力センサー",
      text_28: "有効電力",
      paragraph_1: "温度設定です",
      paragraph_2: "開始時間です",
      paragraph_3: "終了時間です",
    },
  };
function showOrHide(dom,type){
   if(type===true){
       $('#'+dom).show();
   }else{
       $('#'+dom).hide();
   }
}
function getRearCabinTemperature() {
    // 获取本地缓存的后舱温度
    const settingSwitchObject = serialLocalStorage.get("settingSwitchObject")
    $('#current_wendu_houcang').text(settingSwitchObject.rear_cabin_temperature_switch)
}

function initSettingConfig() {
    // 获取本地缓存的开关配置
    const settingSwitchObject = serialLocalStorage.get("settingSwitchObject")
    console.log("[初始化设置页面的开关数据]"+ JSON.stringify(settingSwitchObject));
    // 后仓温度
    $('#current_wendu_qiancang').text(getLocalTemperature("front_cabin_temperature"))

    // 前舱温度
    $('#current_wendu_houcang').text(getLocalTemperature("rear_cabin_temperature"))

    // 岩盘温度
    $('#current_wendu_yanpan').text(getLocalTemperature("rock_temperature"))

    // 前舱开关
    if (getLocalTemperatureSwitch("front_cabin_temperature_switch")) {
        showOrHide('qiancang_open',false)
        showOrHide('qiancang_close',true)
    } else {
        showOrHide('qiancang_open',true)
        showOrHide('qiancang_close',false)
    }

    // 后舱开关
    let houcang_status  = getLocalTemperatureSwitch("rear_cabin_temperature_switch")
    console.log("[后舱开关状态]"+houcang_status)
    console.log("[后舱开关状态]"+parseInt(houcang_status))
    if (houcang_status) {
        console.log("[后舱开]"+ settingSwitchObject.rear_cabin_temperature_switch);
        showOrHide('houcang_close',true)
        showOrHide('houcang_open',false)
    } else {
        showOrHide('houcang_open',true)
        showOrHide('houcang_close',false)
        console.log("[后舱关]"+ settingSwitchObject.rear_cabin_temperature_switch);
    }

    // 岩盘开关
    if (getLocalTemperatureSwitch("rock_temperature_switch")) {
        showOrHide('yanpan_close',true)
        showOrHide('yanpan_open',false)
    } else {
        showOrHide('yanpan_open',true)
        showOrHide('yanpan_close',false)
    }

    // UV开关
    if (getLocalTemperatureSwitch("uvc_switch")) {
        showOrHide('uv_close',true)
        showOrHide('uv_open',false)
    } else {
        showOrHide('uv_open',true)
        showOrHide('uv_close',false)
    }

    // 负离子开关
    if (getLocalTemperatureSwitch("negative_ion_switch")) {
        showOrHide('fulizi_close',true)
        showOrHide('fulizi_open',false)
    } else {
        showOrHide('fulizi_open',true)
        showOrHide('fulizi_close',false)
    }
    //初始化语言格式
    $("#language_select .selected").removeClass("selected")
    // 从浏览器获取语言类型
    const languageType = serialLocalStorage.get("languageType")
    $("span[data-type="+languageType+"]").parent(".flex-col").addClass("selected")
    // 拿到语言包中的对应类型的文字
    const languageObj = languageData[languageType];
    // 拿到对应的数据类,利用返回的数组进行替换语言类型
    Object.keys(languageObj).forEach((i)=>{
        // i为对应的文字类,替换文字
        $("." + i).text(languageObj[i])
    })
}

// 回调函数
setTimeout(function () {
    // 初始化开关配置
    initSettingConfig();
}, 500);
$('#to_home').click(()=>{
    localStorage.setItem('isReturn','1')
    window.location.href = './index.html'
})

$('#uv_open').click(()=>{
    showOrHide('uv_close',true)
    showOrHide('uv_open',false)
    setUVCSwitch(true)
})
$('#uv_close').click(()=>{
    showOrHide('uv_open',true)
    showOrHide('uv_close',false)
    setUVCSwitch(false)
})

$('#houcang_open').click(()=>{
    showOrHide('houcang_close',true)
    showOrHide('houcang_open',false)
    $('#current_wendu_houcang').text(getLocalTemperature("rear_cabin_temperature"))
    setRearCabinTemperatureSwitch(true)
})
$('#houcang_close').click(()=>{
    showOrHide('houcang_open',true)
    showOrHide('houcang_close',false)
    setRearCabinTemperatureSwitch(false)
})

$('#qiancang_open').click(()=>{
    showOrHide('qiancang_close',true)
    showOrHide('qiancang_open',false)
    $('#current_wendu_qiancang').text(getLocalTemperature("front_cabin_temperature"))
    setFrontCabinTemperatureSwitch(true)
})
$('#qiancang_close').click(()=>{
    showOrHide('qiancang_open',true)
    showOrHide('qiancang_close',false)
    setFrontCabinTemperatureSwitch(false)
})

$('#yanpan_open').click(()=>{
    showOrHide('yanpan_close',true)
    showOrHide('yanpan_open',false)
    $('#current_wendu_yanpan').text(getLocalTemperature("rock_temperature"))
    setRockTemperatureSwitch(true)
})
$('#yanpan_close').click(()=>{
    showOrHide('yanpan_open',true)
    showOrHide('yanpan_close',false)
    setRockTemperatureSwitch(false)
})
$('#fulizi_open').click(()=>{
    showOrHide('fulizi_close',true)
    showOrHide('fulizi_open',false)
    $('#current_wendu_yanpan').text(getLocalTemperature("rock_temperature"))
    setNegativeIonSwitch(true)
})
$('#fulizi_close').click(()=>{
    showOrHide('fulizi_open',true)
    showOrHide('fulizi_close',false)
    setNegativeIonSwitch(false)
})

//注册 语言按钮的 语言切换的点击事件
$("#language_select span").click(function (event) {
    // 设置按钮切换背景
    $("#language_select .selected").removeClass("selected")
    $(this).parent("div").addClass("selected")
    //获取语言类型
    const languageType = $(this).data("type")
    // 拿到语言包中的对应类型的文字
    const languageObj = languageData[languageType];
    // 拿到对应的数据类,利用返回的数组进行替换语言类型
    Object.keys(languageObj).forEach((i)=>{
        // i为对应的文字类,替换文字
        // if (!i.includes("text_")) {
        //     $("." + i).text(languageObj[i])
        // }else{
            $("." + i).text(languageObj[i])
        // }
    })

    // 设置语言缓存至浏览器
    serialLocalStorage.put("languageType",languageType)

})

function getCurrentTimeFormatted(){
    const now = new Date(); // 获取当前时间
    const year = now.getFullYear(); // 年
    const month = String(now.getMonth() + 1).padStart(2, '0'); // 月，注意要加1
    const day = String(now.getDate()).padStart(2, '0'); // 日
    const hours = String(now.getHours()).padStart(2, '0'); // 时
    const minutes = String(now.getMinutes()).padStart(2, '0'); // 分
    const seconds = String(now.getSeconds()).padStart(2, '0'); // 秒

    // 拼接成需要的格式
    $('#text_2').text(`${year}/${month}/${day} ${hours}:${minutes}:${seconds}`)
}

setInterval(()=>{
    getCurrentTimeFormatted();
},1000)
